<?php

namespace Home\Controller;

/**
 * 游戏管理-牛牛&三公
 *
 * @author Administrator
 *        
 */
class NiuNiuSanGongController extends BaseController {
	/**
	 */
	function __construct() {
	}
	/**
	 * 获取配置
	 */
	public function mb_GetConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_GetConfig', array () ) );
		if ($retData->m_Result != ErrCode_Normal) {
			$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
			return;
		}
		
		$jData = json_decode ( $retData->m_Data );
		
		$config = array (
				'game' => ( int ) $gameData ['status'],
				'coinvalue' => ( string ) $jData->coinvalue,
				'robots' => $jData->robots 
		);
		if (isset ( $jData->duivalue ))
			$config ['duivalue'] = $jData->duivalue;
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $config ) );
	}
	/**
	 * 设置配置
	 */
	public function mb_SetConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$status = $data->status;
		$coinValue = $data->coinvalue;
		$duiValue = $data->duivalue;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_SetConfig', array (
				'coinvalue' => $coinValue,
				'duivalue' => $duiValue 
		) ) );
		if ($retData->m_Result != ErrCode_Normal) {
			$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
			return;
		}
		
		$gameInfo->save ( array (
				'id' => $gameId,
				'status' => $status 
		) );
		
		$lobbyAppInfo = M ( 'Appinfo', '', 'DB_LOBBY' );
		$lobbyAppInfo->save ( array (
				'id' => $gameId,
				'status' => $status 
		) );
		\WMHandler::mb_GetInstance ()->mb_UpdateGameStatus ( $gameId, $status );
		
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 操作机器人
	 */
	public function mb_HandleRobotName() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$type = $data->type;
		$key = $data->key; // add/remove
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_HandleRobotName', array (
				'type' => $type,
				'key' => $key 
		) ) );
		if ($retData->m_Result != ErrCode_Normal) {
			$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
			return;
		}
		$this->mb_OutMessage ( ErrCode_Normal, ErrMsg_Normal );
	}
	/**
	 * 获取大区配置
	 */
	public function mb_GetZoneConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$zoneId = $data->zoneid;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_GetZoneConfig', array (
				'zoneid' => $zoneId 
		) ) );
		$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
	}
	/**
	 * 设置大区配置
	 */
	public function mb_SetZoneConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$zoneId = $data->zoneid;
		$count = $data->count;
		$enter = $data->enter;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_SetZoneConfig', array (
				'zoneid' => $zoneId,
				'count' => $count,
				'enter' => $enter 
		) ) );
		$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
	}
	/**
	 * 获取房间配置
	 */
	public function mb_GetRoomConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$zoneId = $data->zoneid;
		$roomNum = $data->roomnum;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_GetRoomConfig', array (
				'zoneid' => $zoneId,
				'roomnum' => $roomNum 
		) ) );
		$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
	}
	/**
	 * 设置房间配置
	 */
	public function mb_SetRoomConfig() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$zoneId = $data->zoneid;
		$roomNum = $data->roomnum;
		$difficulty = $data->difficulty;
		$chouShui = $data->choushui;
		$robotCount = $data->robotcount;
		$betDiff = $data->betdiff;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_SetRoomConfig', array (
				'zoneid' => $zoneId,
				'roomnum' => $roomNum,
				'difficulty' => $difficulty,
				'choushui' => $chouShui,
				'robotcount' => $robotCount,
				'betdifficulty' => $betDiff 
		) ) );
		$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
	}
	/**
	 * 设置控杀信息
	 */
	public function mb_SetControlInfo() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$zoneId = $data->zoneid;
		$roomNum = $data->roomnum;
		$controlId = $data->controlid;
		$controlDifficulty = $data->controldifficulty;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_SetControlInfo', array (
				'zoneid' => $zoneId,
				'roomnum' => $roomNum,
				'controlid' => $controlId,
				'controldifficulty' => $controlDifficulty 
		) ) );
		$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
	}
	/**
	 * 获取游戏记录
	 */
	public function mb_GetRecord() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		$type = $data->type;
		$date_0 = $data->date0;
		$date_1 = $data->date1;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$params = array (
				'type' => $type,
				'date0' => $date_0,
				'date1' => $date_1 
		);
		if ($type == 'zoneroom') {
			$params ['zoneid'] = $data->zoneid;
			$params ['roomnum'] = $data->roomnum;
			$params ['id'] = $data->id;
			$params ['name'] = $data->name;
		} else if ($type == 'control') {
			$params ['id'] = $data->id;
			$params ['name'] = $data->name;
		} else if ($type == 'name') {
			$params ['id'] = $data->id;
			$params ['name'] = $data->name;
		} else if ($type == 'num') {
			$params ['id'] = $data->id;
			$params ['name'] = $data->name;
			$params ['num'] = $data->roomnum;
		}
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_GetRecord', $params ) );
		$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
	}
	public function mb_TotalAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_TotalAccount', array () ) );
		$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
	}
	public function mb_GetOnlinePlayers() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$gameId = $data->gameid;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$gameInfo = M ( 'Gameinfo' );
		$gameData = $gameInfo->where ( "id='$gameId'" )->find ();
		$gameUrl = $gameData ['url'];
		
		$retData = $this->mb_DecodeWMData ( \WMGameHandler::mb_GetInstance ()->mb_CommonCMD ( $gameUrl, 'mb_GetOnlinePlayers', array () ) );
		$this->mb_OutMessage ( $retData->m_Result, $retData->m_Data );
	}
	/**
	 * 解析游戏服务端返回数据
	 *
	 * @param unknown $reqData        	
	 * @return \RepData|mixed
	 */
	private function mb_DecodeWMData($reqData) {
		$data = new \RepData ();
		if ($reqData == null || $reqData == '') {
			$data->m_Result = ErrCode_Toast;
			$data->m_Data = '外部服务器异常，请稍后再试';
			return $data;
		}
		$reqJson = json_decode ( $reqData );
		if (json_last_error () != JSON_ERROR_NONE) {
			$data->m_Result = ErrCode_Toast;
			$data->m_Data = '外部服务器异常，请稍后再试';
			return $data;
		}
		return $reqJson;
	}
}